﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Xml.Linq;

namespace BACKOFFICE
{
    public partial class SeatingChart : DevExpress.XtraEditors.XtraUserControl
    {
        private Control activeControl;
        private Point previousLocation;

        int HabachiBtnNumber = 1;
        int DinningBtnNumber = 1;
        int PrivateBtnNumber = 1;
        int BarBtnNumber = 1;
        int SushiBarBtnNumber = 1;

        public SeatingChart()
        {
            InitializeComponent();

            
            if (File.Exists("SeatingChart.xml"))
            {
                DialogResult result = MessageBox.Show("Do you want to use saved seating chart?", "Confirmation", MessageBoxButtons.YesNo);

                if (result == DialogResult.Yes)
                {
                    // read in the entire document
                    var document = XDocument.Load("SeatingChart.xml");

                    var champs = document.Descendants("SimpleButton")
                        .Select(e => new { text = e.Element("Text").Value, X = e.Element("X").Value, Y = e.Element("Y").Value, Width = e.Element("Width").Value, Height = e.Element("Height").Value });

                    foreach (var champ in champs)
                    {
                        SimpleButton test = new SimpleButton();
                        int x = 1;
                        //int locationXextra = (340 / 2) + 50;
                        test.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                        test.Name = "btn" + x.ToString();
                        test.Text = champ.text;
                        test.Width = Convert.ToInt32(champ.Width);
                        test.Height = Convert.ToInt32(champ.Height);
                        test.Location = new Point(Convert.ToInt32(champ.X), Convert.ToInt32(champ.Y));
                        test.MouseDown += new MouseEventHandler(btn_MouseDown);
                        test.MouseMove += new MouseEventHandler(btn_MouseMove);
                        test.MouseUp += new MouseEventHandler(btn_MouseUp);
                        test.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                        test.Appearance.FontStyleDelta = FontStyle.Bold;
                        panelControl1.Controls.Add(test);
                    }
                }
                else
                {
                    SimpleButton btnOpenTicket = new SimpleButton();
                    btnOpenTicket.Name = "btnOpenTicket";
                    btnOpenTicket.Text = "OPEN TICKETS";
                    btnOpenTicket.Width = 75;
                    btnOpenTicket.Height = 75;
                    btnOpenTicket.Location = new Point(5, 5);
                    btnOpenTicket.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                    btnOpenTicket.MouseDown += new MouseEventHandler(btn_MouseDown);
                    btnOpenTicket.MouseMove += new MouseEventHandler(btn_MouseMove);
                    btnOpenTicket.MouseUp += new MouseEventHandler(btn_MouseUp);
                    panelControl1.Controls.Add(btnOpenTicket);
                }

            }
            else
            {
                SimpleButton btnOpenTicket = new SimpleButton();
                btnOpenTicket.Name = "btnOpenTicket";
                btnOpenTicket.Text = "OPEN TICKETS";
                btnOpenTicket.Width = 75;
                btnOpenTicket.Height = 75;
                btnOpenTicket.Location = new Point(5, 5);
                btnOpenTicket.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                btnOpenTicket.MouseDown += new MouseEventHandler(btn_MouseDown);
                btnOpenTicket.MouseMove += new MouseEventHandler(btn_MouseMove);
                btnOpenTicket.MouseUp += new MouseEventHandler(btn_MouseUp);
                panelControl1.Controls.Add(btnOpenTicket);
            }
        }

        void btn_MouseDown(object sender, MouseEventArgs e)
        {
            activeControl = sender as Control;
            previousLocation = e.Location;
            Cursor = Cursors.Hand;
        }

        void btn_MouseMove(object sender, MouseEventArgs e)
        {
            if (activeControl == null || activeControl != sender)
                return;

            var location = activeControl.Location;
            location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
            activeControl.Location = location;
        }

        void btn_MouseUp(object sender, MouseEventArgs e)
        {
            activeControl = null;
            Cursor = Cursors.Default;
        }

        private void btnDinningSeat_Click(object sender, EventArgs e)
        {
            var btnDinning = new SimpleButton();
            btnDinning.Width = 75;
            btnDinning.Height = 75;
            btnDinning.Text = "Dinning " + DinningBtnNumber.ToString();
            btnDinning.Location = new Point(100, 100);
            btnDinning.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnDinning.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnDinning.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnDinning);
            DinningBtnNumber++;
        }

        private void btnHabachiTable_Click(object sender, EventArgs e)
        {
            var btnHabachi = new SimpleButton();
            btnHabachi.Width = 75;
            btnHabachi.Height = 75;
            btnHabachi.Name = "btnHabachi" + HabachiBtnNumber.ToString();
            btnHabachi.Text = "Habachi " + HabachiBtnNumber.ToString();
            btnHabachi.Location = new Point(100, 100);
            btnHabachi.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnHabachi.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnHabachi.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnHabachi);
            HabachiBtnNumber++;
        }

        private void btnBarSeat_Click(object sender, EventArgs e)
        {
            var btnBarSeat = new SimpleButton();
            btnBarSeat.Width = 50;
            btnBarSeat.Height = 50;
            btnBarSeat.Text = "Bar " + BarBtnNumber.ToString();
            btnBarSeat.Location = new Point(100, 100);
            btnBarSeat.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnBarSeat.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnBarSeat.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnBarSeat);
            BarBtnNumber++;
        }

        private void btnSushiBarSeat_Click(object sender, EventArgs e)
        {
            var btnSushiBarSeat = new SimpleButton();
            btnSushiBarSeat.Width = 50;
            btnSushiBarSeat.Height = 50;
            btnSushiBarSeat.Text = "Sushi " + SushiBarBtnNumber.ToString();
            btnSushiBarSeat.Location = new Point(100, 100);
            btnSushiBarSeat.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnSushiBarSeat.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnSushiBarSeat.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnSushiBarSeat);
            SushiBarBtnNumber++;
        }

        private void btnPrivateRoom_Click(object sender, EventArgs e)
        {
            var btnPrivate = new SimpleButton();
            btnPrivate.Width = 75;
            btnPrivate.Height = 75;
            btnPrivate.Text = "Private " + PrivateBtnNumber.ToString();
            btnPrivate.Location = new Point(100, 100);
            btnPrivate.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnPrivate.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnPrivate.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnPrivate);
            PrivateBtnNumber++;
        }

        private void btnSaveSeatingChart_Click(object sender, EventArgs e)
        {
            SaveLoadXMLSeattingChart.buttons.Clear();

            foreach (Control controls in panelControl1.Controls)
            {
                SaveLoadXMLSeattingChart.buttons.Add(controls);
            }

            SaveLoadXMLSeattingChart.WriteToXml();

            DialogResult result = MessageBox.Show("Do you want to Send this to all Stations", "QUESTION", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                string filePath = Environment.CurrentDirectory + @"\" + StaticProperties.TableChartFileName;
                string FileName = StaticProperties.TableChartFileName;

                FileSendRecieveClass.SendFiles.SendFile(filePath, FileName);
            }
            else
            {

            }
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
